local xinghan = fk.CreateSkill {

  name = "joy__xinghan",

  tags = {  },

}



xinghan:addEffect(fk.TurnStart, {
  name = "joy__xinghan",
    anim_type = "special",
    priority = 2,
    events = {fk.TurnStart,fk.DamageInflicted},
    can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(xinghan.name) or player:usedSkillTimes(xinghan.name, Player.HistoryTurn) ~= 0 then return false end
    local room = player.room
    local ta = player.room:getTag(xinghan.name)
    local kin
    if type(ta) == "table" then
       kin = ta
      for _, p in ipairs(player.room.alive_players) do
        table.insertIfNeed(kin,p.kingdom)
      end
      room:setTag(xinghan.name,kin)
    else
       kin = {"wei","wu","qun","god"}
      for _, p in ipairs(player.room.alive_players) do
        table.insertIfNeed(kin,p.kingdom)
      end
      room:setTag(xinghan.name,kin)
    end
    table.removeOne(kin,"shu")
    local mark = type(player:getMark(xinghan.name)) == "table" and player:getMark(xinghan.name) or {}
    for _, k in ipairs(kin) do
      if not table.contains(mark,k) then
        return  target == player and player:hasSkill(xinghan.name)  and player:usedSkillTimes(xinghan.name, Player.HistoryTurn) == 0
      end
    end
  end,
    on_cost = function(self, event, target, player, data)
      return  player.room:askForSkillInvoke(player,xinghan.name,nil,"兴汉：你可以发动一次“定乱”，成功后你增加一点体力上限和体力，并将场上对应势力角色全部修改为蜀势力。")
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local ta = player.room:getTag(xinghan.name)
      if type(ta) == "table" then
        local kin = ta
        for _, p in ipairs(player.room.alive_players) do
          table.insertIfNeed(kin,p.kingdom)
        end
        room:setTag(xinghan.name,kin)
      else
        local kin = {"wei","wu","qun","god"}
        for _, p in ipairs(player.room.alive_players) do
          table.insertIfNeed(kin,p.kingdom)
        end
        room:setTag(xinghan.name,kin)
      end
      local mark = type(player:getMark(xinghan.name)) == "table" and player:getMark(xinghan.name) or {}
      local cs = player.room:getTag(xinghan.name)
      table.removeOne(cs,"shu")
      local asc = table.simpleClone(cs)
      if #mark > 0 then
        for _, c in ipairs(mark) do
          table.removeOne(cs,c)
        end
      end
      if #cs <= 0 then return end
      local choice = room:askForChoice(player,cs,"定乱","选择要进行“定乱”的势力。",false,asc)
      room:changeMaxHp(player,1)
      if player:isWounded() and not player.dead then
        room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = xinghan.name
        }
      end
      if choice then
        local mark = type(player:getMark(xinghan.name)) == "table" and player:getMark(xinghan.name) or {}
        table.insert(mark,choice)
        room:setPlayerMark(player,xinghan.name,mark)
        for _, p in ipairs(room.alive_players) do
            if p.kingdom == choice then
                room:changeKingdom(p, "shu", true)
            end
        end
      end
    end,
})
xinghan:addEffect(fk.DamageInflicted, {
  name = "joy__xinghan",
    anim_type = "special",
    priority = 2,
    events = {fk.TurnStart,fk.DamageInflicted},
    can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(xinghan.name) or player:usedSkillTimes(xinghan.name, Player.HistoryTurn) ~= 0 then return false end
    local room = player.room
    local ta = player.room:getTag(xinghan.name)
    local kin
    if type(ta) == "table" then
       kin = ta
      for _, p in ipairs(player.room.alive_players) do
        table.insertIfNeed(kin,p.kingdom)
      end
      room:setTag(xinghan.name,kin)
    else
       kin = {"wei","wu","qun","god"}
      for _, p in ipairs(player.room.alive_players) do
        table.insertIfNeed(kin,p.kingdom)
      end
      room:setTag(xinghan.name,kin)
    end
    table.removeOne(kin,"shu")
    local mark = type(player:getMark(xinghan.name)) == "table" and player:getMark(xinghan.name) or {}
    for _, k in ipairs(kin) do
      if not table.contains(mark,k) then
        return  target == player and player:hasSkill(xinghan.name)  and player:usedSkillTimes(xinghan.name, Player.HistoryTurn) == 0
      end
    end
  end,
    on_cost = function(self, event, target, player, data)
      return  player.room:askForSkillInvoke(player,xinghan.name,nil,"兴汉：你可以发动一次“定乱”，成功后你增加一点体力上限和体力，并将场上对应势力角色全部修改为蜀势力。")
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local ta = player.room:getTag(xinghan.name)
      if type(ta) == "table" then
        local kin = ta
        for _, p in ipairs(player.room.alive_players) do
          table.insertIfNeed(kin,p.kingdom)
        end
        room:setTag(xinghan.name,kin)
      else
        local kin = {"wei","wu","qun","god"}
        for _, p in ipairs(player.room.alive_players) do
          table.insertIfNeed(kin,p.kingdom)
        end
        room:setTag(xinghan.name,kin)
      end
      local mark = type(player:getMark(xinghan.name)) == "table" and player:getMark(xinghan.name) or {}
      local cs = player.room:getTag(xinghan.name)
      table.removeOne(cs,"shu")
      local asc = table.simpleClone(cs)
      if #mark > 0 then
        for _, c in ipairs(mark) do
          table.removeOne(cs,c)
        end
      end
      if #cs <= 0 then return end
      local choice = room:askForChoice(player,cs,"定乱","选择要进行“定乱”的势力。",false,asc)
      room:changeMaxHp(player,1)
      if player:isWounded() and not player.dead then
        room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = xinghan.name
        }
      end
      if choice then
        local mark = type(player:getMark(xinghan.name)) == "table" and player:getMark(xinghan.name) or {}
        table.insert(mark,choice)
        room:setPlayerMark(player,xinghan.name,mark)
        for _, p in ipairs(room.alive_players) do
            if p.kingdom == choice then
                room:changeKingdom(p, "shu", true)
            end
        end
      end
    end,
})

return xinghan